package com.cip.admin.action;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cip.admin.model.Sms;
import com.cip.admin.service.SMSService;
import com.cip.webapp.action.BaseAction;
import com.cip.master.service.AcademicService;
import com.cip.master.service.ClassYearService;
import com.cip.master.service.SectionService;
import com.cip.master.service.ExaminationService;

import com.cip.master.model.Academic;
import com.cip.master.model.ClassYear;
import com.cip.master.model.Section;
import com.cip.master.model.Examination;

public class SMSAction extends BaseAction{

	/**
	 * 
	 */
	public static final Log logger = LogFactory.getLog(com.cip.admin.action.SMSAction.class);
	private static final long serialVersionUID = 1L;
	private SMSService smsService;
	private Sms sms;
	private File fileUpload;
	private AcademicService  academicService;
	private ClassYearService classYearService;
	private ExaminationService examService;
	private SectionService sectionService;
	
	private String messageText;
	//private List<Academic> academicList;
	//private List<Examination> examList;
	private List<ClassYear> classYears;
	private List<Section> sectionList; 
	
	//private String academicYear;
	private String classYear;
	private String sectionName;
	//private String examName;
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String showSMSCreate() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String showUploadContact() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String showBroadcastSMS() {
		try {
			setClassYears(classYearService.getAllClassYear());
			setSectionList(sectionService.getAllSection());
		} catch (Exception e) {
			logger.error(getText("errors.sms.broadcast")+e.getMessage());
			addActionError(getText("errors.sms.broadcast")+e.getMessage());			
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String sentSMS() {
		try {
			smsService.sentSMS(sms);
			saveMessage(getText("sms.added"));
		} catch (Exception e) {
			logger.error(getText("errors.sms")+e.getMessage());
			addActionError(getText("errors.sms")+e.getMessage());	
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String broadcastSms(){
		
		logger.info("sent sms to all TextMessage : "+getMessageText());
		
		try {
			smsService.broadcastSms(getMessageText());
			saveMessage(getText("sms.added"));
		} catch (Exception e) {
			logger.error(getText("errors.sms")+e.getMessage());
			addActionError(getText("errors.sms")+e.getMessage());
		}
		
		return SUCCESS;
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String broadcastSmsSelect() {
		Map<String,String> smsBroadCastSelectParam = new  HashMap<String, String>();
		logger.info("broadcastsms for classYear : "+classYear+"sectionName : "+sectionName+"messageText : "+messageText);
		
		try {
			smsBroadCastSelectParam.put("classYear", classYear);
			smsBroadCastSelectParam.put("sectionName", sectionName);
			smsBroadCastSelectParam.put("messageText", getMessageText());
			smsService.broadcastSmsSelect(smsBroadCastSelectParam);
			saveMessage(getText("sms.added"));
		} catch (Exception e) {
			logger.error(getText("errors.sms")+e.getMessage());
			addActionError(getText("errors.sms")+e.getMessage());
		}
		
		return SUCCESS;
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String sentUploadContact(){
		Map<String,String> smsParam = new  HashMap<String, String>();
		smsParam.put("messageText", getMessageText());
		logger.info("sent sms to upload TextMessage : "+getMessageText());
		
		try {
			smsService.sentUploadSms(smsParam,fileUpload);
		} catch (Exception e) {
			logger.error(getText("errors.sms")+e.getMessage());
			addActionError(getText("errors.sms")+e.getMessage());
		}
		
		return SUCCESS;
	}
	
	/**
	 * Getter and Setter
	 */
	
	public void setSectionService(SectionService sectionService) {
		this.sectionService = sectionService;
	}

	public SectionService getSectionService() {
		return sectionService;
	}
	
	public ExaminationService getExamService() {
		return examService;
	}

	public void setExamService(ExaminationService examService) {
		this.examService = examService;
	}
	
	public ClassYearService getClassYearService() {
		return classYearService;
	}

	public void setClassYearService(ClassYearService classYearService) {
		this.classYearService = classYearService;
	}
	
	public AcademicService getAcademicService() {
		return academicService;
	}

	public void setAcademicService(AcademicService academicService) {
		this.academicService = academicService;
	}
	public File getFileUpload() {
		return fileUpload;
	}

	public void setFileUpload(File fileUpload) {
		this.fileUpload = fileUpload;
	}

	/*private String getExamName(){
		return examName;
	}
	
	public void setExamName(String examName) {
		this.examName = examName;
	}
	
	private String getAcademicYear(){
		return academicYear;
	}
	
	public void setAcademicYear(String academicYear) {
		this.academicYear = academicYear;
	}*/
	
	public String getClassYear() {
		return classYear;
	}

	public void setClassYear(String classYear) {
		this.classYear = classYear;
	}

	public String getSectionName() {
		return sectionName;
	}

	public void setSectionName(String sectionName) {
		this.sectionName = sectionName;
	}

	public String getMessageText() {
		return messageText;
	}
	
	public void setMessageText(String messageText) {
		this.messageText = messageText;
	}

	public Sms getSms() {
		return sms;
	}
	
	public void setSms(Sms sms) {
		this.sms = sms;
	}
	
	public SMSService getSmsService() {
		return smsService;
	}
	
	public void setSmsService(SMSService smsService) {
		this.smsService = smsService;
	}
	
/*	public List<Examination> getExamList() {
		return examList;
	}

	public void setExamList(List<Examination> examList) {
		this.examList = examList;
	}
*/
	public List<ClassYear> getClassYears() {
		return classYears;
	}

	public void setClassYears(List<ClassYear> classYears) {
		this.classYears = classYears;
	}
	
	/*public List<Academic> getAcademicList() {
		return academicList;
	}

	public void setAcademicList(List<Academic> academicList) {
		this.academicList = academicList;
	}*/
	
	public List<Section> getSectionList() {
		return sectionList;
	}

	public void setSectionList(List<Section> sectionList) {
		this.sectionList = sectionList;
	}
}
